package com.ruoyi.web.controller.vqms;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.vqms.domain.VQMSStorehouse;
import com.ruoyi.vqms.domain.VQMSStorehouseLocation;
import com.ruoyi.vqms.service.IVQMSFactoryService;
import com.ruoyi.vqms.service.IVQMSQueueAreaService;
import com.ruoyi.vqms.service.IVQMSStorehouseLocationService;
import com.ruoyi.vqms.service.IVQMSStorehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库库位 控制器
 */
@RestController
@RequestMapping("/vqms/storehouseLocation")
public class VQMSStorehouseLocationController extends BaseController {
    @Autowired
    private IVQMSStorehouseLocationService storehouseLocationService;
    @Autowired
    private IVQMSQueueAreaService queueAreaService;

    /**
     * 查询仓库库位列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('vqms:storehouseLocation:list')")
    @RequestMapping("/getList")
    public AjaxResult getList(VQMSStorehouseLocation vqmsStorehouseLocation){
        return AjaxResult.success(storehouseLocationService.selectList(vqmsStorehouseLocation));
    }

    /**
     * 查询仓库库位列表
     */
    @PreAuthorize("@ss.hasPermi('vqms:storehouseLocation:list')")
    @RequestMapping("/list")
    public TableDataInfo list(VQMSStorehouseLocation vqmsStorehouseLocation){
        startPage();
        List<VQMSStorehouseLocation> list=storehouseLocationService.selectList(vqmsStorehouseLocation);
        return getDataTable(list);
    }

    /**
     * 新增仓库库位
     */
    @Log(title = "新增仓库库位", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('vqms:storehouseLocation:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VQMSStorehouseLocation vqmsStorehouseLocation){
        VQMSStorehouseLocation old=storehouseLocationService.getByName(vqmsStorehouseLocation.getName());
        if(old !=null){
            return AjaxResult.error("数据库已存在同名库位，请更改库位名称！");
        }
        vqmsStorehouseLocation.setCreateBy(SecurityUtils.getUsername());
        storehouseLocationService.add(vqmsStorehouseLocation);
        return AjaxResult.success();
    }

    /**
     * 查询仓库库位
     */
    @PreAuthorize("@ss.hasPermi('vqms:storehouseLocation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(storehouseLocationService.getById(id));
    }

    /**
     * 修改仓库库位
     */
    @Log(title = "修改仓库库位", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('vqms:storehouseLocation:update')")
    @PutMapping(value = "/update")
    public AjaxResult update(@RequestBody VQMSStorehouseLocation vqmsStorehouseLocation)
    {
        vqmsStorehouseLocation.setUpdateBy(SecurityUtils.getUsername());
        storehouseLocationService.update(vqmsStorehouseLocation);
        return AjaxResult.success();
    }

    /**
     * 删除仓库库位
     */
    @Log(title = "删除仓库库位", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('vqms:storehouse:delete')")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        for(Long locationId : ids) {
            int num = queueAreaService.countByLocationId(locationId);
            if (num > 0) {
                return AjaxResult.error("删除失败，请先删除该库位和排队队列的绑定关系！");
            }
        }
        storehouseLocationService.delete(ids);
        return AjaxResult.success();
    }

}
